/*
 * 项目名称:platform-plus
 * 类名称:NcpPersonInfoServiceImpl.java
 * 包名称:com.platform.modules.ncp.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-02-11 18:03:21        tumin     初版做成
 *
 * Copyright (c) 2019-2019 微同软件
 */
package com.platform.modules.ncp.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.common.utils.StringUtils;
import com.platform.modules.ncp.dao.NcpPersonInfoDao;
import com.platform.modules.ncp.entity.NcpPersonInfoEntity;
import com.platform.modules.ncp.service.NcpPersonInfoService;
import com.platform.modules.utils.ExcelUtil;
import com.platform.modules.utils.ShiroUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Service实现类
 *
 * @author tumin
 * @date 2020-02-11 18:03:21
 */
@Service("ncpPersonInfoService")
public class NcpPersonInfoServiceImpl extends ServiceImpl<NcpPersonInfoDao, NcpPersonInfoEntity> implements NcpPersonInfoService {

    @Override
    public List<NcpPersonInfoEntity> queryAll(Map<String, Object> params) {
        params.put("createUserId",ShiroUtils.getUserId());
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.PERS_ID");
        params.put("asc", false);
        params.put("createUserId",ShiroUtils.getUserId());
        Page<NcpPersonInfoEntity> page = new Query<NcpPersonInfoEntity>(params).getPage();
        return page.setRecords(baseMapper.selectNcpPersonInfoPage(page, params));
    }

    @Override
    public boolean add(NcpPersonInfoEntity ncpPersonInfo) {
        ncpPersonInfo.setCreateTime(new Date());
        ncpPersonInfo.setCreateUserId(ShiroUtils.getUserId());
        ncpPersonInfo.setCreateUserOrgNo(ShiroUtils.getUserEntity().getOrgNo());
        return this.save(ncpPersonInfo);
    }

    @Override
    public boolean update(NcpPersonInfoEntity ncpPersonInfo) {
        return this.updateById(ncpPersonInfo);
    }

    @Override
    public boolean delete(String persId) {
        return this.removeById(persId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] persIds) {
        return this.removeByIds(Arrays.asList(persIds));
    }

    /**
     * @Author TuMin
     * @Description 导出excel
     * @Date 10:52 2020/2/12
     * @Param [ncpPersonInfoEntities, url]
     * @return void
     */
    @Override
    public Workbook exportExcel(Map<String,Object> param){
        List<NcpPersonInfoEntity> ncpPersonInfoEntities = queryAll(param);
        Workbook workbook = ExcelUtil.exportPersonInfo(ncpPersonInfoEntities);
        return workbook;
    }

    /**
     * @Author TuMin
     * @Description 导入excel
     * @Date 17:11 2020/2/12
     * @Param [file]
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean importExcel(MultipartFile file) {
        List<NcpPersonInfoEntity> ncpPersonInfoEntities = ExcelUtil.importExcel(file, 1, 2, NcpPersonInfoEntity.class);
        Map<String,Object> param = new HashMap<>();
        List<NcpPersonInfoEntity> personInfoEntities = this.queryAll(param);
        List<NcpPersonInfoEntity> queryList = new ArrayList<>();
        if (personInfoEntities.size() == 0){
            ncpPersonInfoEntities.forEach(entity -> {
                entity.setCreateTime(new Date());
                entity.setCreateUserId(ShiroUtils.getUserId());
                entity.setCreateUserOrgNo(ShiroUtils.getUserEntity().getOrgNo());
                queryList.add(entity);
                if (entity.getPersName().equals("请将疾控平台下发的名单和街道自行排查的名单进行区分标识。")){
                    queryList.remove(entity);
                }
            });
        }else {
            for (NcpPersonInfoEntity oldEntity : personInfoEntities) {
                ncpPersonInfoEntities.forEach(newEntity ->{
                    if (oldEntity.getIdNumber().equals(newEntity.getIdNumber())){
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
                        if (dateFormat.format(oldEntity.getCreateTime()).equals(dateFormat.format(new Date()))){
                            newEntity.setPersId(oldEntity.getPersId());
                        }else {
                            newEntity.setCreateTime(new Date());
                            newEntity.setCreateUserId(ShiroUtils.getUserId());
                            newEntity.setCreateUserOrgNo(ShiroUtils.getUserEntity().getOrgNo());
                        }
                    } else {
                        newEntity.setCreateTime(new Date());
                        newEntity.setCreateUserId(ShiroUtils.getUserId());
                        newEntity.setCreateUserOrgNo(ShiroUtils.getUserEntity().getOrgNo());
                    }
                    queryList.add(newEntity);
                    if (newEntity.getPersName().equals("请将疾控平台下发的名单和街道自行排查的名单进行区分标识。")){
                        queryList.remove(newEntity);
                    }
                });
            }
        }
        ArrayList<NcpPersonInfoEntity> collect = queryList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(NcpPersonInfoEntity::getIdNumber))), ArrayList::new));
        return this.saveOrUpdateBatch(collect);
    }
}
